package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Employee;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceimpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> list() {
        return employeeMapper.selectList(null);
    }

    // 实现登录方法
    @Override
    public Employee login(int id, String password) {
        Employee employee = employeeMapper.findById(Integer.valueOf(id));
        if (employee != null && employee.getPassword().equals(password)) {
            return employee;
        }
        return null;
    }

    @Override
    public Employee getEmployeeById(int id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public Employee updateEmployee(int id, Employee employee) {
        employee.setId(id);
        employeeMapper.updateById(employee);
        return employee;
    }

    @Override
    public boolean register(Employee employee) {
        try {
            // 检查用户 ID 是否已存在
            Employee existingUser = employeeMapper.findById(Integer.valueOf(employee.getId()));
            if (existingUser != null) {
                return false; // 用户 ID 已存在，注册失败
            }
            employeeMapper.insert(employee);
            return true;
        } catch (Exception e) {
            System.err.println("注册员工信息时出现异常：" + e.getMessage());
            return false;
        }
    }
}